<?php

namespace App\Admin\Controllers\Invoicing;


use App\Admin\Actions\Grid\Tools\SwtProcurement\AddProcurement;
use App\Admin\Actions\Grid\Tools\SwtProcurement\MakeInbound;
use App\Admin\Actions\Grid\Row\SwtProcurement\ProcurementDetail;
use App\Admin\Actions\Grid\Row\SwtProcurement\StartProcurement;
use App\Admin\Renderable\GoodsRenderable;
use App\Admin\Repositories\SwtGood;
use App\Admin\Repositories\SwtProcurement;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;
use Ramsey\Uuid\Uuid;

class SwtProcurementController extends AdminController
{
    protected $title = '采购入库';

    /**
     * 查看采购单列表
     * return
     */
    protected function grid()
    {
        return Grid::make(new SwtProcurement(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('procurement_no','采购单号');
            $grid->column('procurement_type','状态')->using(['关闭','预采购','采购中','采购完成'])->label(['danger','success','info','warning']);
            $grid->column('procurement_total','采购单总金额') -> display(function ($value) {
                return "￥".bcdiv($value, 100 ,2);
            });
            $grid->column('create_name','申请人');

            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('procurement_no','采购单号');
                $filter->between('created_at');
            });


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                //采购单详情按钮
                $actions -> prepend(new ProcurementDetail());

                if($actions->row->procurement_type == 1) {
                    //开始采购 按钮
                    $actions -> prepend(new StartProcurement());
                    $actions -> disableDelete(false);
                }

                if($actions->row->procurement_type == 2) {
                    //入库 按钮
                    $actions -> append(new MakeInbound());
                }

            });

            $grid->quickSearch('procurement_no');
            $grid->disableCreateButton(false);
            $grid->model()->orderBy('id', 'desc');

        });
    }



    /**
     * 新增采购单
     * return
     */
    protected function form()
    {
        return Form::make(new SwtProcurement(), function (Form $form) {

            $form->text('remake','备注');

            $form->hidden('procurement_no');
            $form->hidden('procurement_type');
            $form->hidden('create_id');
            $form->hidden('create_name');
            $form->hidden('procurement_total');

            $form->hasMany('goods','添加商品',function ($form) {

                $form->row(function (Form\Row $form) {
                    $form->width(4)
                        -> selectTable('good_id','商品名')
                        -> title('商品表')
                        -> from(GoodsRenderable::make())
                        -> rules('required', [ 'required' => '商品名不能为空' ])
                        -> model(SwtGood::class, 'id', 'goods_name')
                        -> required();

                    $form ->width(3)
                        -> text('advance_quantity','采购数量')
                        -> type('number')
                        -> rules('required', [ 'required' => '数量不能为空'])
                        -> required();

                    $form ->width(3)
                        -> text('advance_price','采购价格')-> type('number')
                        -> rules('required', [ 'required' => '价格不能为空'])
                        -> required();
                });

            }) -> saving(function ($data) {
                if(count($data) > 0) {
                    $list = [];
                    foreach($data as $v) {
                        $v['advance_price'] = bcmul($v['advance_price'],100,2);
                        $list[] = $v;
                    }
                }
                return $list;
            });


            $form->saving(function (Form $form) {
                $total = 0;
                foreach($form->goods as $good) {
                    $money = $good['advance_price'] * $good['advance_quantity'];
                    $total += $money;
                }

                $form->input('create_id',Admin::user()->id);
                $form->input('create_name',Admin::user()->username);
                $form->input('procurement_total',bcmul($total,100,2));
                $form->input('procurement_no','CG'.strtoupper(MD5(Uuid::uuid4() -> toString())));
                $form->input('procurement_type',1);
            });

        });
    }



}
